#include "spi.h"
#include "lcd.h"
#include "main.h"
#include <stdint.h>


uint8_t HZK[] =
{
    0x10, 0x60, 0x02, 0x8C, 0x00, 0x0E, 0x22, 0x12, 0x0A, 0xC2, 0x0A, 0x12, 0x22, 0x0E, 0x00, 0x00,
    0x04, 0x04, 0x7E, 0x01, 0x20, 0x21, 0x11, 0x09, 0x05, 0xFF, 0x05, 0x09, 0x11, 0x21, 0x20, 0x00, /*"深",0*/

    0x20, 0x20, 0x20, 0xFF, 0x20, 0x20, 0x00, 0xFF, 0x00, 0x00, 0xFE, 0x00, 0x00, 0xFF, 0x00, 0x00,
    0x10, 0x30, 0x10, 0x0F, 0x08, 0x88, 0x60, 0x1F, 0x00, 0x00, 0x3F, 0x00, 0x00, 0xFF, 0x00, 0x00, /*"圳",1*/

    0x00, 0x00, 0xFE, 0x92, 0x92, 0x92, 0x92, 0xFE, 0x92, 0x92, 0x92, 0x92, 0xFE, 0x00, 0x00, 0x00,
    0x40, 0x38, 0x01, 0x00, 0x3C, 0x40, 0x40, 0x42, 0x4C, 0x40, 0x40, 0x70, 0x05, 0x08, 0x30, 0x00, /*"思",2*/

    0x40, 0x40, 0x42, 0xCC, 0x00, 0x04, 0x04, 0x04, 0xFC, 0x24, 0x24, 0x24, 0x24, 0xE4, 0x04, 0x00,
    0x00, 0x40, 0x20, 0x1F, 0x20, 0x50, 0x48, 0x46, 0x41, 0x40, 0x48, 0x50, 0x48, 0x47, 0x40, 0x00, /*"迈",3*/

    0x10, 0x88, 0xC4, 0x33, 0x80, 0x9E, 0x90, 0x9F, 0x90, 0x9E, 0x20, 0xD8, 0x17, 0xF0, 0x10, 0x00,
    0x01, 0x00, 0xFF, 0x80, 0x40, 0x3E, 0x02, 0x02, 0x3E, 0x10, 0x88, 0x67, 0x18, 0x67, 0x80, 0x00, /*"微",4*/

    0x00, 0x00, 0xF8, 0x88, 0x88, 0x88, 0x88, 0xFF, 0x88, 0x88, 0x88, 0x88, 0xF8, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x1F, 0x08, 0x08, 0x08, 0x08, 0x7F, 0x88, 0x88, 0x88, 0x88, 0x9F, 0x80, 0xF0, 0x00, /*"电",5*/

    0x80, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0xE2, 0xA2, 0x92, 0x8A, 0x86, 0x82, 0x80, 0x80, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x80, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /*"子",6*/

    0x04, 0x04, 0x04, 0x84, 0xE4, 0x3C, 0x27, 0x24, 0x24, 0x24, 0x24, 0xE4, 0x04, 0x04, 0x04, 0x00,
    0x04, 0x02, 0x01, 0x00, 0xFF, 0x09, 0x09, 0x09, 0x09, 0x49, 0x89, 0x7F, 0x00, 0x00, 0x00, 0x00, /*"有",7*/

    0x00, 0xFE, 0x22, 0x5A, 0x86, 0x00, 0xFE, 0x92, 0x92, 0x92, 0x92, 0x92, 0xFE, 0x00, 0x00, 0x00,
    0x00, 0xFF, 0x04, 0x08, 0x07, 0x00, 0xFF, 0x40, 0x20, 0x03, 0x0C, 0x14, 0x22, 0x41, 0x40, 0x00, /*"限",8*/

    0x00, 0x80, 0x40, 0x20, 0x18, 0x06, 0x80, 0x00, 0x07, 0x18, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00,
    0x01, 0x00, 0x20, 0x70, 0x28, 0x26, 0x21, 0x20, 0x20, 0x24, 0x38, 0x60, 0x00, 0x01, 0x01, 0x00, /*"公",9*/

    0x00, 0x10, 0x12, 0x92, 0x92, 0x92, 0x92, 0x92, 0x92, 0x12, 0x12, 0x02, 0xFE, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x3F, 0x10, 0x10, 0x10, 0x10, 0x3F, 0x00, 0x40, 0x80, 0x7F, 0x00, 0x00, 0x00, /*"司",10*/
};

uint8_t SMWDZ[] =
{
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
};


//di=0, write command
//di=1,write data
void write( uint8_t _data, uint8_t di )
{
    uint8_t data = _data;

    if( 0 == di )
    {
        HAL_GPIO_WritePin( LCD_CMD_GPIO_Port, LCD_CMD_Pin, GPIO_PIN_RESET );
    }
    else
    {
        HAL_GPIO_WritePin( LCD_CMD_GPIO_Port, LCD_CMD_Pin, GPIO_PIN_SET );
    }

    HAL_GPIO_WritePin( LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET );
    HAL_SPI_Transmit( &hspi1, ( uint8_t * )&data, 1, 1000 );
    HAL_GPIO_WritePin( LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_SET );
    HAL_GPIO_WritePin( LCD_CMD_GPIO_Port, LCD_CMD_Pin, GPIO_PIN_SET );
}

void init_ads( uint8_t page )
{
    write( page, 0 ); //set page address
    write( 0x10, 0 ); //Set Higer Column Address
    write( 0x00, 0 ); //Set Lower Column Address
}

void display( uint8_t a, uint8_t b )
{
    uint8_t pag = 0xb0;
    uint8_t i, j;

    write( 0x40, 0 );           //Set Display Start Line
    
    for( i = 0; i < 8; i++ )    //8 pages
    {
        init_ads( pag );
        
        for( j = 0; j < 64; j++ )
        {
            write( a, 1 );
            write( b, 1 );
        }
        
        pag++;
    }

    HAL_Delay( 800 );
}

void disp_HZ16( uint8_t uc_RowNo, uint8_t uc_ColNo, uint8_t uc_HZCode )
{
    uint8_t uc_PageCnt, uc_ColCnt;
    uint8_t uc_PageAddr;

    uc_PageAddr = 0xB0 | ( uc_RowNo * 2 );

    for ( uc_PageCnt = 0; uc_PageCnt < 2; uc_PageCnt++ )
    {
        write( uc_PageAddr, 0 ); //Set Page Address

        write( 0x10 | ( ( uc_ColNo >> 4 ) & 0x0F ), 0 ); //Set Upper Bit Column Address
        write( 0x00 | ( uc_ColNo & 0x0F ), 0 ); //Set Low Bit Column Address

        for ( uc_ColCnt = 0; uc_ColCnt < 16; uc_ColCnt++ )
        {
            write( HZK[uc_HZCode * 32 + uc_PageCnt * 16 + uc_ColCnt], 1 );
        }
        
        uc_PageAddr ++;
    }
}

void disp_HZ16str( uint8_t uc_RowNo, uint8_t uc_ColNo, uint8_t *HZinf, uint8_t uc_HZCnt )
{
    uint8_t uc_CharCnt;

    for ( uc_CharCnt = 0; uc_CharCnt < uc_HZCnt; uc_CharCnt++ )
    {
        disp_HZ16( uc_RowNo, uc_ColNo, HZinf[uc_CharCnt] );
        uc_ColNo = uc_ColNo + 16;
    }
}

void initial_lcd( void )
{
    HAL_GPIO_WritePin( LCD_CS_GPIO_Port, LCD_CS_Pin, GPIO_PIN_RESET );
    HAL_Delay( 20 ); //2ms
    HAL_GPIO_WritePin( LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_RESET );
    HAL_Delay( 400 ); //40ms
    HAL_GPIO_WritePin( LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_SET );
    HAL_Delay( 100 ); //10ms

    write( 0xe2, 0 ); //Reset
    HAL_Delay( 10 ); //1ms
    write( 0xA2, 0 ); //set 1/9 bias
    write( 0xA0, 0 ); //seg0~seg131
    write( 0xc8, 0 ); //com63~com0

    write( 0x24, 0 ); //set (1+Rb/Ra) 100:5.0 ; 111: 6.4
    HAL_Delay( 20 ); //2ms
    write( 0x81, 0 ); //Electronic Volume Mode Set
    write( 0x2B, 0 ); //Electronic Volume Register Set (改变此值可调节对比度，调节范围0x00~0x3F）
    HAL_Delay( 10 ); //1ms

    write( 0x29, 0 ); //power control set
    HAL_Delay( 10 ); //1ms
    write( 0x2B, 0 ); //power control set
    HAL_Delay( 10 ); //1ms
    write( 0x2F, 0 ); //power control set
    HAL_Delay( 10 ); //1ms

    write( 0xaf, 0 ); //Dispaly On
    HAL_Delay( 10 ); //1ms
}


#if 0
void main( void )
{

    initial();
    while ( 1 )
    {

        display( 0xff, 0xff );

        display( 0x00, 0x00 );

        display( 0xaa, 0x55 );

        disp_HZ16str( 2, 0, SMWDZ, 5 );
        HAL_Delay( 800 );

    }
}
#endif



























